﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Diagnostics; 
using Rally; 

namespace GameEngine
{
    public class Ground : EngineEntity
    { 
        EngineModel model;
        EngineModel sky;

        public Ground(Game game,List<ObjectProperty> properties)
            : base(game,properties)
        {


        }

        Texture2D _textureStaticLighting = null;


        List<EnginePointlight> lights = new List<EnginePointlight>();

        public override void DrawLights()
        {
            foreach (EnginePointlight light in lights)
            {
                Render.RenderLight(light);
            }
        }

        private void onLoadLight(EnginePointlight light)
        {
            lights.Add(light);
        } 
        private void onLoadSubobject(string Name, Matrix transform, List<ObjectProperty> properties)
        {
            

        }

        private void onLoadProperties(List<ObjectProperty> properties)
        {
            foreach (ObjectProperty property in properties)
            {
                if (property.Name == "lightmap")
                {
                    Texture tmp;
                    if (EngineResource.LoadTexture(".\\Content\\Textures\\" + property.Value, out tmp))
                    {
                        if (_textureStaticLighting != null)
                            _textureStaticLighting.Dispose();
                        if(tmp is Texture2D)
                        _textureStaticLighting = tmp as Texture2D;
                    }
                }
            }
        }

        protected override void LoadContent()
        {
            try
            {
                model = new EngineModel();
                if (IsReloading)
                    model.Create(".\\Content\\Models\\container.mdl", onLoadProperties, null, null, null);
                else
                    model.Create(".\\Content\\Models\\container.mdl", onLoadProperties, onLoadLight, onLoadSubobject, null);

                sky = new EngineModel();
                sky.Create(".\\Content\\Models\\sky.mdl", null,null,null,null);
                base.LoadContent();
            }
            catch (Exception e)
            {
                UnloadContent();
            }
        }

        protected override void UnloadContent()
        {

            if (model != null)
            {
                model.Release();
                model = null;
            }

            if (_textureStaticLighting != null)
            {
                _textureStaticLighting.Dispose();
                _textureStaticLighting = null;
            }
 
            base.UnloadContent();
        }
 

        public override void Draw(GameTime gameTime)
        {
            Render.GraphicsDevice.Textures[1] = _textureStaticLighting;
            sky.Draw(Matrix.Identity);
            model.Draw(Matrix.Identity);
            base.Draw(gameTime);
        }
    }
}
